package org.gary.core.factories.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.gary.admin.service.LoggerService;
import org.gary.core.factories.BasicDaoFactory;
import org.gary.core.factories.BasicServiceFactory;
import org.gary.core.util.Utils;
import org.springframework.stereotype.Component;

import lengreen.core.interfaces.Entity;

/**
 * 对Service 工厂实现类
 * @author lengreen
 *
 * @param <E> 泛型传入一个entity
 */
@Component("basicServiceFactory")
public class BasicServiceFactoryImpl<E> implements BasicServiceFactory<E> {
	private BasicDaoFactory<E> basicDaoFactory;
	/**记录日志的Service*/
	private LoggerService loggerService;
	
	public String save(E entity) {
		if(entity != null) {
			loggerService.makeLog(entity.getClass().getName(), "start save a entity, " + entity);
			getBasicDaoFactory().save(entity);
			loggerService.makeLog(entity.getClass().getName(), "save a entity success, " + entity);
		}
		return null;
	}

	public String update(Entity entity) {
		if (entity.getId() > 0) {
			loggerService.makeLog(entity.getClass().getName(), "start update a entity, " + entity);
			getBasicDaoFactory().update(entity);
			loggerService.makeLog(entity.getClass().getName(), "update a entity success, " + entity);
		}
		return null;
	}
	
	public String delete(Entity entity) {
		if (entity.getId() > 0) {
			loggerService.makeLog(entity.getClass().getName(), "start delete a entity, " + entity);
			getBasicDaoFactory().delete(entity);
			loggerService.makeLog(entity.getClass().getName(), "delete a entity success, " + entity);
		}
		return null;
	}

	public void delete(Class<E> entity, Object[] ids) {
		loggerService.makeLog(entity.getName(), "start delete some entitys, " + Utils.arrayToString(ids));
		basicDaoFactory.delete(entity, ids);
		loggerService.makeLog(entity.getName(), "delete entitys success, " + Utils.arrayToString(ids));
	}

	public List<E> list(Class<E> entity) {
		return basicDaoFactory.list(entity);
	}
	
	public List<E> list(Class<E> entity,int top,int page,Object lang,int type) {
		return basicDaoFactory.list(entity, top, page, lang, type);
	}
	
	public List<E> list(Class<E> entity, String groupBy) {
		return basicDaoFactory.list(entity, groupBy);
	}

	public List<E> list(Class<E> entity, int pageNo, int maxCount,
			Object[] orderBy) {
		return basicDaoFactory.list(entity, pageNo, maxCount, orderBy);
	}

	public List<E> list(Class<E> entity, Map<String, Object> where,
			Object[] orderBy) {
		return basicDaoFactory.list(entity, where, orderBy);
	}

	public List<E> list(Class<E> entity, int pageNo, int maxCount,
			Map<String, Object> where, Object[] orderBy) {
		return basicDaoFactory.list(entity, pageNo, maxCount, where, orderBy);
	}

	public List<E> list(Class<E> entity, int pageNo, int maxCount,
			Map<String, Object> where, String groupBy, Object[] orderBy) {
		return basicDaoFactory.list(entity, pageNo, maxCount, where, groupBy,
				orderBy);
	}

	public E load(Class<E> entity, int id) {
		return basicDaoFactory.load(entity, id);
	}

	@Override
	public E load(Class<E> entity, int id, Object lang) {
		return basicDaoFactory.load(entity, id, lang);
	}
	
	public int selectTotalCount(Class<E> entity) {
		return basicDaoFactory.selectTotalCount(entity);
	}

	public int[] update(Class<E> entity, Object[] ids, boolean visible) {
		loggerService.makeLog(entity.getName(), "start update some entitys, " + Utils.arrayToString(ids));
		int[] result = basicDaoFactory.update(entity, ids, visible);
		loggerService.makeLog(entity.getName(), "update entitys success, " + Utils.arrayToString(ids));
		return result;
	}
	
	@Resource
	public void setBasicDaoFactory(BasicDaoFactory<E> basicDaoFactory) {
		this.basicDaoFactory = basicDaoFactory;
	}

	public BasicDaoFactory<E> getBasicDaoFactory() {
		return basicDaoFactory;
	}

	@Resource
	public void setLoggerService(LoggerService loggerService) {
		this.loggerService = loggerService;
	}

	public LoggerService getLoggerService() {
		return loggerService;
	}

	@Override
	public E load(Class<E> entity, Object lang) {		
		return basicDaoFactory.load(entity, lang);
	}

	@Override
	public E load(Class<E> entity, String name, Object lang) {
		return basicDaoFactory.load(entity, name, lang);
	}

	@Override
	public List<E> list(Class<E> entity, int top, int page, Object lang,
			List<Integer> type) {
		return basicDaoFactory.list(entity, top, page, lang, type);
	}

	@Override
	public List<Integer> getId(int id) {
		return basicDaoFactory.getId(id);
	}

	@Override
	public boolean getParentId(int id) {
		return basicDaoFactory.getParentId(id);
	}

}
